package com.woniuxy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/set")
    public boolean set() {
        System.out.println("set.....");
        //redisTemplate.opsForValue().set("name","zhangsan");
        //设置key同时指定过期时间 单位：秒
        redisTemplate.opsForValue().set("name", "zhangsan", 100);
        return true;
    }

    @RequestMapping("/get")
    public String get() {
        return redisTemplate.opsForValue().get("name").toString();
    }

    @RequestMapping("/exp")
    public boolean expire() {
        //单独指定过期时间
        redisTemplate.expire("name", 20, TimeUnit.SECONDS);
        return true;

    }

    @RequestMapping("/ttl")
    public long ttl() {
        return redisTemplate.getExpire("name");
    }

    @RequestMapping("/del")
    public boolean del() {
        redisTemplate.delete("name");
        return true;
    }


    @RequestMapping(".setMap")
    public boolean setMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "lisi");
        map.put("age", 10);
        redisTemplate.opsForHash().putAll("user", map);

        System.out.println(redisTemplate.opsForHash().get("user", "name"));
        System.out.println(redisTemplate.opsForHash().delete("user", "age"));
        Map<Object, Object> values = redisTemplate.opsForHash().entries("user");

        return true;
    }

    public void list() {
        redisTemplate.opsForList().notify();
    }

    // set集合
    public void set1() {
        redisTemplate.opsForSet();
    }

    public void zset() {
        redisTemplate.opsForZSet().add("热搜", "朝阳区", 111);

    }





}